using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;

using Microsoft.Practices.CompositeUI;
using Microsoft.Practices.CompositeUI.SmartParts;
using Microsoft.Practices.ObjectBuilder;

using LibrErian.Model;

namespace LibrErian.Modules.BasicBook
{
	[SmartPart]
	public partial class SideBarView : UserControl, ISideBarView 
	{
		private SideBarViewPresenter presenter;

		[CreateNew]
		public SideBarViewPresenter Presenter
		{
            set 
            { 
                presenter = value;
                presenter.View = this;
            }
		}
	
		public SideBarView()
		{
			InitializeComponent();
		}

        private void SideBarView_Load(object sender, EventArgs e)
        {
            presenter.OnViewReady();
        }

        public void InitializeTreeview(IList genres, IList authors)
        {
            tvFilter.Nodes.Clear();
            TreeNode allNode = new TreeNode("All");
            tvFilter.Nodes.Add(allNode);
            
            TreeNode genreNode = new TreeNode("Genres");
            foreach (Genre genre in genres)
            {
                TreeNode node = new TreeNode(genre.Name);
                node.Tag = genre;
                genreNode.Nodes.Add(node);
            }
            allNode.Nodes.Add(genreNode);

            TreeNode authorNode = new TreeNode("Authors");
            foreach (Author author in authors)
            {
                TreeNode node = new TreeNode(author.Name);
                node.Tag = author;
                authorNode.Nodes.Add(node);
            }
            allNode.Nodes.Add(authorNode);

            tvFilter.ExpandAll();
        }

        public void InitializeLibraries(IList libraries)
        {
            lbLibraries.DataSource = null;
            lbLibraries.DataSource = libraries;
        }

        public void SetSelectedLibrary(Library library)
        {
            lbLibraries.SelectedItem = library;
        }
        
        private void tvFilter_AfterSelect(object sender, TreeViewEventArgs e)
        {
            if (e.Node != null && e.Node.Tag != null)
            {
                if (e.Node.Tag.GetType() == typeof(Genre))
                {
                    Genre genre = e.Node.Tag as Genre;
                    presenter.FilterByGenre(genre.Id);
                }
                else if (e.Node.Tag.GetType() == typeof(Author))
                {
                    Author author = e.Node.Tag as Author;
                    presenter.FilterByAuthor(author.Id);
                }
                else
                {
                    //TODO: Hier waarschuwing wegschrijven.
                }
            }
            else
            {
                presenter.ClearFilter();
            }
        }

        private void miRefresh_Click(object sender, EventArgs e)
        {
            presenter.Refresh();
        }

        private void lbLibraries_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (lbLibraries.SelectedItem != null)
            {
                Library selectedLibrary = lbLibraries.SelectedItem as Library;
                presenter.OnSelectdLibraryChanged(selectedLibrary);
            }
        }

        private void miNewLibrary_Click(object sender, EventArgs e)
        {
            presenter.OnNewLibrary();
        }

        private void miEditLibrary_Click(object sender, EventArgs e)
        {
            if(lbLibraries.SelectedItem != null)
            {
                Library selectedLibrary = (Library)lbLibraries.SelectedItem;
                presenter.OnEditLibrary(selectedLibrary);
            }
        }

        private void miDeleteLibrary_Click(object sender, EventArgs e)
        {
            if (lbLibraries.SelectedItem != null)
            {
                Library selectedLibrary = (Library)lbLibraries.SelectedItem;
                presenter.OnDeleteLibrary(selectedLibrary);
            }
        }

        private void lbLibraries_MouseUp(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Right)
            {
                int indexOfItemAtClick = lbLibraries.IndexFromPoint(e.Location);
                if (indexOfItemAtClick >= 0)
                {
                    lbLibraries.SelectedIndex = indexOfItemAtClick;
                    Point contextLocation = lbLibraries.PointToScreen(e.Location);
                    cmsLibraries.Show(contextLocation);
                }
            }
        }
    }
}